package com.hang.config;

import com.hang.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
/**
 * 拦截过滤器
 */
public class FilterConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JWTInterceptor())
                .excludePathPatterns("/swagger-resources/**","/webjars/**", "/v2/**","/swagger-ui.html/**")
                .excludePathPatterns("/login/**","/register/**")  // 放行的请求路径
                .addPathPatterns("/**"); // 拦截所有请求路径除了上面放行的
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域设置
        CorsRegistration cors = registry.addMapping("/**");

        // 可访问的外部域
        cors.allowedOrigins("*");
        // 设置 header 能携带的信息
        cors.allowedHeaders("*");
        // 支持跨域的请求方法
        cors.allowedMethods("*");
        // 设置跨域过期时间，单位为秒
        cors.maxAge(3600);
    }
}